package interview.tree;

import java.util.Scanner;

/**
 * ali 满二叉树计数 https://codefun2000.com/p/P1082
 */
public class FullChildTreeNum {

    static long count = 0;
    static int[][] tree;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        tree = new int[n+1][2];
        for (int i = 1;i <= n;i++) {
            tree[i][0] = sc.nextInt();
            tree[i][1] = sc.nextInt();
        }
        getHeight(1);
        System.out.println(count);
    }

    /**
     * 获取树高度，若当前树不为满二叉树（子树不满或子树高度不相等）则返回-1
     */
    public static int getHeight(int root) {
        if (root == -1) return 0;
        int leftH = getHeight(tree[root][0]);
        int rightH = getHeight(tree[root][1]);
        if (leftH == -1 || rightH == -1 || leftH != rightH)
            return -1; //-1表示子树不为满二叉树。若子树不满或者左右子树高度不一致则当前树也不满
        count++;//否则当前树为满二叉树
        return leftH+1;
    }
}
